<template>
	<view class="">
		<u-loading-page :loading="isLoading"></u-loading-page>
		<view class="app" v-show="!isLoading">
			<u-form size="medium" labelWidth="160rpx" ref="shForm" :model="obj" :rules="itemRules" class="edit_form">
				<u-form-item required label="地址名称" prop="name"><u-input placeholder="请输入退货地址名称" v-model="obj.name"></u-input></u-form-item>
				<u-form-item required label="联系人" prop="linkman"><u-input placeholder="请输入联系人" v-model="obj.linkman"></u-input></u-form-item>
				<u-form-item required label="联系号码" prop="phone"><u-input placeholder="请输入手机号" v-model="obj.phone"></u-input></u-form-item>
				<u-form-item label="固定电话"><u-input placeholder="请输入固定电话" v-model="obj.mobile"></u-input></u-form-item>
				<u-form-item label="邮政编码"><u-input maxlength="6" placeholder="请输入邮政编码" v-model="obj.postcode"></u-input></u-form-item>
				<u-form-item required label="所在区域" prop="districtSelected">
					<uni-data-picker
						class="u-flex-1"
						v-model="obj.districtSelected"
						:localdata="options"
						popup-title="请选择所在区域"
						@change="onchange"
						@nodeclick="onnodeclick"
					></uni-data-picker>
				</u-form-item>
				<u-form-item required label="地址" prop="address"><u-input placeholder="请输入具体地址" v-model="obj.address"></u-input></u-form-item>
			</u-form>
			<view class="u-flex u-flex-items-center footer">
				<u-button size="small" shape="circle" @click="cancal">取消</u-button>
				<u-button class="u-m-l-60" type="primary" size="small" shape="circle" @click="saveData">确定</u-button>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
// import Upload from '@/components/Upload/SingleImage';
import {regionData, CodeToText, TextToCode} from 'element-china-area-data';

export default {
	data() {
		//校验手机号码
		let validateMobile = (rule, value, callback) => {
			if (!/^1[23456789]\d{9}$/.test(value)) {
				callback(new Error('手机号码有误，请重填'));
			} else {
				callback();
			}
		};

		return {
			obj: {
				name: '',
				linkman: '',
				phone: '',
				mobile: '',
				postcode: '',
				district: '',
				districtSelected: [],
				address: ''
				//status:0
			},
			_obj: {},
			pageIsEdit: 0, //0->新增,1->编辑
			pageName: '',
			indate: false,
			isLoading: false,
			options: [],
			itemRules: {
				name: [
					{
						required: true,
						message: '请输入退货地址名称'
					}
				],
				linkman: [
					{
						required: true,
						message: '请输入联系人'
					}
				],
				phone: [
					{
						required: true,
						validator: validateMobile
					}
				],
				districtSelected: [
					{
						required: true,
						message: '请选择所在区域'
					}
				],
				address: [
					{
						required: true,
						message: '请输入具体地址'
					}
				]
				// business_license: [{
				//   required: true,
				//   trigger: 'change',
				//   message:'营业执照不能为空'
				// }],
			}
		};
	},
	components: {},
	onLoad(e) {
		regionData.forEach(a => {
			a.text = a.label;
			if (a.children && a.children.length > 0) {
				a.children.forEach(b => {
					b.text = b.label;
					if (b.children && b.children.length > 0) {
						b.children.forEach(c => {
							c.text = c.label;
						});
					}
				});
			}
		});
		this.options = regionData;
		//获取路由参数(编辑时使用)
		if (e.id) {
			//编辑页面
			this.pageIsEdit = 1;
			this.pageName = '编辑退货地址';
			this.getItemData(e.id);
		} else {
			this.pageIsEdit = 0;
			this.pageName = '新增退货地址';
			//this._obj = Object.assign({},this.obj);
		}
	},
	onReady() {
		this.$refs.shForm.setRules(this.itemRules);
	},
	methods: {
		//获取编辑页面基础数据
		getItemData(id) {
			this.isLoading = true;
			uni.$u.http.get('/shop/return/address/' + id,{custom:{id:6}}).then(res => {
				this.isLoading = false;
				let o = res.msg;
				//this.obj = o;

				//处理所在区域
				let _d = [];
				let _t;
				if (o.district.indexOf(',') > -1) {
					_t = o.district.split(',');
				} else {
					_t = o.district.split(' ');
				}
				//拼接所在区域选择数组
				_d[0] = TextToCode[_t[0]].code;
				_d[1] = TextToCode[_t[0]][_t[1]].code;
				_d[2] = TextToCode[_t[0]][_t[1]][_t[2]].code;
				o.districtSelected = _d;

				//赋值
				this.obj = Object.assign({}, o);
			});
		},

		//级联选择区域
		districtSelect(value) {
			let s = '';
			value.forEach(e => {
				s += CodeToText[e] + ',';
			});
			s = s.substring(0, s.length - 1);
			//对象赋值
			this.obj.district = s;
		},
		//保存数据
		saveData() {
			let that = this;
			let _data = Object.assign({}, this.obj);
			this.$refs.shForm.validate().then(valid => {
				if (valid) {
					//删除所在无用字段
					delete _data.districtSelected;
					// #ifndef APP-NVUE
					const eventChannel = this.getOpenerEventChannel();
					// #endif
					//保存数据
					this.isLoading = true;
					switch (this.pageIsEdit) {
						case 0:
							//新增
							uni.$u.http.post('/shop/return/address', _data,{custom:{id:6}}).then(res => {
								this.isLoading = false;
								this.$refs.uToast.show({
									message: '新增成功',
									type: 'success',
									complete: () => {
										eventChannel.emit('savesuccess', {});
										uni.$u.route({
											type: 'back'
										});										
									}
								});
							});
							break;
						case 1:
							//编辑
							//删除字段
							delete _data.status_text;
							uni.$u.http.put('/shop/return/address/' + _data.id, _data,{custom:{id:6}}).then(res => {
								this.isLoading = false;
								let _this = this;
								this.$refs.uToast.show({
									message: '更新成功',
									type: 'success',
									complete: () => {
										eventChannel.emit('savesuccess', {});
										uni.$u.route({
											type: 'back'
										});
									}
								});
								//this.getItemData(this.$route.query.id);
							});
							break;
						default:
							//no code
							break;
					}
				} else {
					//console.log('error')
					return false;
				}
			});
		},
		cancal() {
			uni.$u.route({
				type: 'navigateBack',
				delta: 1
			});
		},
		onchange(e) {
			const value = e.detail.value;
			let s = '';
			value.forEach(e => {
				s += e.text + ',';
			});
			s = s.substring(0, s.length - 1);
			this.obj.district = s;
		},
		onnodeclick(e) {
		}
	}
};
</script>

<style lang="scss">
.app {
	background: #fff;
	padding: 0 30rpx;
}
.footer {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	box-sizing: border-box;
}
</style>
